package com.ipfsemc.ipfsemc.controller.v1;

import com.ipfsemc.ipfsemc.model.Bounty;
import com.ipfsemc.ipfsemc.service.IBountyService;
import com.ipfsemc.ipfsemc.swagger.ApiModelPropertyIgnore;
import com.ipfsemc.ipfsemc.util.JsonData;
import com.ipfsemc.ipfsemc.util.RespList;
import com.ipfsemc.ipfsemc.util.Response;
import com.ipfsemc.ipfsemc.util.ResponseBase;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("bounty")

@Api(value = "BountyController", tags = "奖励金")
public class BountyController {
    private static final Logger logger = LoggerFactory.getLogger(BountyController.class);

    @Autowired
    private IBountyService bountyService;
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户ID",required=true,dataType="String")
    })
    @ApiOperation(value = "用户奖励金明细",httpMethod = "GET")
    @RequestMapping(value = "listBounty",method = RequestMethod.GET)
    public RespList<Bounty> listBounty(String userId){
        RespList jsonData=new RespList();

        try {
            if (null ==userId) {
                jsonData.setMessage("参数有误");
            }else{
                Bounty bounty =new Bounty();
                bounty.setUserId(userId);
                List<Bounty> bounties = bountyService.listBounty(bounty);

                jsonData.setCode(0);
                jsonData.setResult(bounties);
            }

        } catch (Exception ex) {
            logger.error("[bounty][listBounty]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }


    @ApiOperation(value = "提现明细",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户ID",required=true,dataType="String")
    })
    @RequestMapping(value = "listBountyState",method = RequestMethod.GET)
    public RespList<Bounty> listBountyState(String userId){
        RespList jsonData=new RespList();

        try {
            if (null ==userId) {
                jsonData.setMessage("参数有误");
            }else{
                Bounty bounty =new Bounty();
                bounty.setUserId(userId);
                List<Bounty> bounties = bountyService.listBountyState(bounty);

                jsonData.setCode(0);
                jsonData.setResult(bounties);
            }

        } catch (Exception ex) {
            logger.error("[bounty][listBountyState]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    @ApiOperation(value = "提现",httpMethod = "POST")

    @PostMapping("addBounty")
    public ResponseBase addBounty(
            @ApiModelPropertyIgnore(
                    {"proNum","proName","price","bountyId","bountyTime","reason","bnakId","bountyState","recomAward"})
            @RequestBody Bounty bounty){
        ResponseBase jsonData=new ResponseBase();

        try {
            if (null ==bounty.getUserId()||null==bounty.getBountyMoney()||null==bounty.getBountyType()) {
                jsonData.setMessage("参数有误");
            }else{
                bounty.setBountyState(-1);
                int i = bountyService.addBounty(bounty);
                if (i > 0) {
                    jsonData.setCode(0);
                    jsonData.setMessage("操作成功！");
                }else if(i==-1){
                    jsonData.setMessage("奖励金余额不足！！");
                }

            }

        } catch (Exception ex) {
            logger.error("[bounty][addBounty]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    @ApiOperation(value = "删除励金明细",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="bountyId",value="ID",required=true,dataType="String")
    })
    @PostMapping("delBounty")
    public ResponseBase delBounty(String bountyId){
        ResponseBase jsonData=new ResponseBase();

        try {
            if (null ==bountyId) {
                jsonData.setMessage("参数有误");
            }else{
                int i = bountyService.delBounty(bountyId);
                if (i > 0) {
                    jsonData.setCode(0);
                    jsonData.setMessage("操作成功！");
                }else {
                    jsonData.setMessage("操作失败！！");
                }

            }

        } catch (Exception ex) {
            logger.error("[bounty][delBounty]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }


}
